import React, { Component } from 'react';
import { HashRouter as Router, Route, Redirect } from 'react-router-dom'
import Home from './pages/home'
import CityList from './pages/citylist'
//解构连接方法
import { connect } from 'react-redux'
//引入action的初始化城市方法
import { initCityAsync } from './store/actionCreator/index'

class App extends Component {

  componentDidMount() {
    this.props.initCity()
  }
  render() {
    return <div >
      <Router >
        <section >
          <Route exact path='/' >
            {/* 重定向有path和to属性 */}
            <Redirect exact to='/home/index' />
          </Route>

          <Route path='/home' component={Home} />
          <Route exact path='/citylist' component={CityList} />

        </section>
      </Router>
    </div>
  }
}
//映射对象
const mapStateToProps = (state) => {
  return {
    cityName: state.cityReducer.cityName
  }
}
//映射方法
const mapDispatchToProps = (dispatch) => {
  return {
    initCity() {
      dispatch(initCityAsync())
    }
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);